import { ADDUSER, DELETEUSER, UPDATEUSER } from "../action/userAction";

function uuid() {
    return Math.random().toString(36).slice(-6);
}

const initialState = [
    { uid: uuid(), name: "用户1", age: 18 },
    { uid: uuid(), name: "用户2", age: 19 }
]

export const userReducer = (state = initialState, { type, payload }) => {
    switch (type) {
        case ADDUSER:
            return [...state, payload];
        case DELETEUSER:
            return state.filter(item => item.uid !== payload)
        case UPDATEUSER:
            return state.map(item => item.uid === payload.uid ? { ...item, ...payload } : item)
        default:
            return state
    }
}
